/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.io.File;
import java.util.*;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

/**
 *
 * @author Admin
 */
public class ArticleReader {

    XMLOutputter printer = null;
    File anXmlDocFile;

    public ArticleReader() {
        //lets create an XML Printer!
        printer = new XMLOutputter();
    }

    public ArticleReader(File xmlFile) {
        //lets create an XML Printer!
        printer = new XMLOutputter();
        this.anXmlDocFile = xmlFile;
    }

    public List<Article> Read(String cate, int ipage) {
        List<Article> ds = new ArrayList<Article>();
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            List<Element> chuyenmuc = domain.getChildren("chuyenmuc");
            Element e = null;
            for (int i = 0; i < chuyenmuc.size(); i++) {
                if (chuyenmuc.get(i).getAttributeValue("name").equals(cate) == true) {
                    List<Element> pages = chuyenmuc.get(i).getChildren("page");
                    for (int j = 0; j < pages.size(); j++) {
                        if (Integer.parseInt(pages.get(j).getAttributeValue("number")) == ipage) {
                            e = pages.get(j);
                        }
                    }
                }
            }

            List<Element> page_ = e.getChildren();
            for (int i = 0; i < page_.size(); i++) {
                Element baibao = page_.get(i);
                Article a = new Article(baibao.getChildTextTrim("link"), baibao.getChildTextTrim("title"), baibao.getChildTextTrim("img"), baibao.getChildTextTrim("date"), baibao.getChildTextTrim("summary"));
                ds.add(a);
            }

        } catch (Exception e) {
            
        }
        return ds;
    }

    public List<Element> LayChuyenmuc() {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            return domain.getChildren("chuyenmuc");
        }catch(Exception e){}
        return null;
    }    
    public String LayURL(String name)
    {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            List<Element> ll= domain.getChildren("chuyenmuc");
            for(int i=0;i<ll.size();i++)
            {
                if(ll.get(i).getAttributeValue("name").equals(name))
                {
                    return ll.get(i).getAttributeValue("url");
                }
            }
        }catch(Exception e){}
        return null;
    }
  }
